package com.fuja.fumo.handler;
import com.fuja.fumo.model.Option;
import com.fuja.fumo.model.Question;
import com.fuja.swf.io.IOUtil;

import waba.io.File;
import waba.util.Vector;
public class ExportHandler {
	private File file;
	public boolean process(Vector questions,Criteria criteria){
		String path = IOUtil.getRoot() + "mockito.html";
		file = new File(path);
		if (file.exists())
			file.delete();
		
		file = new File(path,File.CREATE);
		try {
			writeln("<html><table border=\"1\">");			
			for (int i=0;i<questions.size();i++){
				Question question = (Question) questions.items[i]; 
				if (criteria.process(question)){
					toHTML(question);
				}
			}
			writeln("</table></html>");
		}catch(Exception e){
			return false;
		} finally {
			file.close();
		}
		return true;
	}
	public boolean process(Vector questions){
		return process(questions,new Criteria(){});
	}
	private void toHTML(Question question){
		write("<tr><th colspan=\"2\" align=\"left\">" + question.getId() + ") ");
		writeln(question.getDescription());
		for (int i=0;i<question.getOptions().size();i++){
			Option o = (Option)question.getOptions().items[i];
			toHTML(o);
		}
		writeln("</th></tr>");
	}
	private void toHTML(Option option){
		writeln("<tr><td>" + option.getValue() + "</td>");
		writeln("<td>" + option.getDescription());
		writeln("</td></tr>");
	}
	private void writeln(String text){
		write(text);
		write("\r\n");
	}
	private void write(String text){
		//ds.writeString(text);
		byte[] aux = text.getBytes();
		file.writeBytes(aux, 0, aux.length);
	}	
}

